package com.hanbang.chu_xing_gao_zhao.view_find;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.chu_xing_gao_zhao.entity.FindGangData;
import com.hanbang.chu_xing_gao_zhao.myview.ListViewForScrollView;
import com.hanbang.chu_xing_gao_zhao.myview.MorePopupWindowPublic;
import com.hanbang.chu_xing_gao_zhao.myview.MorePopupWindowPublic.OnClickCallback;
import com.hanbang.chu_xing_gao_zhao.utils.UiUtils;
import com.hanbang.chu_xing_gao_zhao.view_city_select.CitySelectActivity;
import com.hanbang.cxgz_2.R;

import java.util.ArrayList;

public class FindGangFragment extends Fragment implements OnClickListener {
    /**
     * 底部导航按钮-发现
     */
    private LinearLayout find;
    /**
     * 中部导航按钮-城市
     */
    private LinearLayout city;

    /**
     * 中部导航按钮-菜系
     */
    private LinearLayout styleOfCooking;

    /**
     * listView
     */
    private ListViewForScrollView list;

    /**
     * 瀑布流数据集合
     */
    private ArrayList<FindGangData> listData;
    private FindMainFragmentActivity findMainFragmentActivity;
    private View view;

    public FindGangFragment() {

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        findMainFragmentActivity = (FindMainFragmentActivity) getActivity();
        view = inflater.inflate(R.layout.find_gang_activity, null);

        initView();
        setOnClickListener();
        initData();
        return view;
    }


    /**
     * /** 初始化view
     */
    private void initView() {

        city = (LinearLayout) view.findViewById(R.id.ll_bar_mid_conver_classify_city);
        styleOfCooking = (LinearLayout) view.findViewById(R.id.ll_bar_mid_conver_gang_styleOfCooking);
        list = (ListViewForScrollView) view.findViewById(R.id.find_gang_listView);
        find = (LinearLayout) view.findViewById(R.id.bar_botton_bar_find);
        listData = new ArrayList<FindGangData>();

    }

    /**
     * recyclerView 添加临时数据
     */
    private void initData() {

        for (int i = 0; i < 8; i++) {
            listData.add(new FindGangData(R.drawable.ic_launcher, "北京火锅群", i + "123", "指数",
                    "简介：我们生活中指的“菜”通常是能做副食品的植物，如蔬菜；或经过烹调供下饭下酒的蔬菜、蛋品、鱼、肉等。"));
        }

        list.setAdapter(new ListViewAdapter(getActivity(), listData));


    }

    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        city.setOnClickListener(this);
        styleOfCooking.setOnClickListener(this);

    }

    /**
     * 监听处理
     */
    @Override
    public void onClick(View v) {

        Intent intent = new Intent();
        MorePopupWindowPublic popup = null;
        switch (v.getId()) {

            // 选择城市
            case R.id.ll_bar_mid_conver_classify_city:

                intent.setClass(getActivity(), CitySelectActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivityForResult(intent, 99);

                break;
            // 菜系
            case R.id.ll_bar_mid_conver_gang_styleOfCooking:

                popup = new MorePopupWindowPublic(getActivity(), R.layout.item_popupwindow_public, findMainFragmentActivity.styleOfCookingList, 8);
                popup.showPopupWindow(styleOfCooking.getChildAt(0), 2);

            default:

                break;
        }

        // 点击监听的处理
        if (popup != null) {

            popup.setOnClickCallback(new OnClickCallback() {

                @Override
                public void onClick(MorePopupWindowPublic morePopWindow, Object number, String content) {

                    for (int i = 0; i < morePopWindow.parentView.size(); i++) {
                        if ((Integer) number == i) {
                            Toast.makeText(getActivity(), content, Toast.LENGTH_SHORT).show();
                        }
                    }

                }
            });
        }

    }


    /**
     * adapter 设置
     *
     * @author Administrator
     */
    class ListViewAdapter extends BaseAdapter {

        public ArrayList<FindGangData> list;
        private LayoutInflater inflater;

        public ListViewAdapter(Context context, ArrayList<FindGangData> listData) {
            this.inflater = LayoutInflater.from(context);
            if (listData != null) {
                this.list = listData;
            } else {
                list = new ArrayList<FindGangData>();
            }
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public FindGangData getItem(int position) {

            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder;
            if (convertView == null) {// 是空加载tiem
                convertView = inflater.inflate(R.layout.item_find_gang_listview, null);
                UiUtils.applyFont(convertView);
                viewHolder = new ViewHolder();

                // 子控件初始化
                viewHolder.portraitId = (ImageView) convertView.findViewById(R.id.item_find_gang_portrait);
                viewHolder.groupName = (TextView) convertView.findViewById(R.id.item_find_gang_groupName);
                viewHolder.number = (TextView) convertView.findViewById(R.id.item_find_number);
                viewHolder.indext = (TextView) convertView.findViewById(R.id.item_find_index);
                viewHolder.brief = (TextView) convertView.findViewById(R.id.item_find_brief);

                convertView.setTag(viewHolder);// 把现有有item对象存起来
            } else {// 不是空复用

                viewHolder = (ViewHolder) convertView.getTag();
            }

            final FindGangData data = getItem(position);

            viewHolder.portraitId.setImageResource(data.getPoritraitId());
            viewHolder.groupName.setText(data.getGroupName());
            viewHolder.number.setText(data.getNumber());
            viewHolder.indext.setText(data.getIndex());
            viewHolder.brief.setText(data.getBrief());

            /**
             * 头像点击跳转
             */
            viewHolder.portraitId.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                }
            });

            return convertView;
        }

        class ViewHolder {// 进行缓存保存
            // 子控件主要是里面的数据
            private ImageView portraitId;// 肖像图片
            private TextView groupName;
            private TextView number;
            private TextView indext;// 指数
            private TextView brief;// 简介

        }

    }

}
